package com.kexin.common.utils;

import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @author chen
 * @date 2020/6/3
 */
@Component
public class HttpClient {


    private static final int count =5;
    private static SimpleClientHttpRequestFactory requestFactory  =null;
    private static List<RestTemplate> restTemplateList = new ArrayList<>(count);;
    private static final Random random = new Random();
    private static SimpleClientHttpRequestFactory getRequestFactory(){
        if (requestFactory == null){
            requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setConnectTimeout(10000);// 设置超时10s
            requestFactory.setReadTimeout(100000);//100s
        }
        return requestFactory;
    }

    static {
        for (int i =0; i<count;i++ ){
            RestTemplate rest = new RestTemplate(getRequestFactory());
            rest.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
            rest.getMessageConverters().set(2, new StringHttpMessageConverter(Charset.forName("GBK")));
            restTemplateList.add(rest);
        }
    }
    public static RestTemplate getRestTemplate(){
        int index = random.nextInt(count);
        return restTemplateList.get(index);
    }

    public ResponseEntity<String>  post(String url,HttpEntity<MultiValueMap<String, Object>> httpEntity) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        ResponseEntity<String> response = getRestTemplate().exchange(url, HttpMethod.POST, httpEntity, String.class);
        return response;
    }

    public ResponseEntity<String>  post(String url, Map<String,Object> paramMap) {
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
        if (paramMap !=null){
            for (String key : paramMap.keySet()){
                map.add(key,paramMap.get(key));
            }
        }
        HttpHeaders headers = new HttpHeaders();
        //  请勿轻易改变此提交方式，大部分的情况下，提交方式都是表单提交
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(map,headers);
        ResponseEntity<String> response = getRestTemplate().exchange(url, HttpMethod.POST, httpEntity, String.class);
        return response;
    }

    public ResponseEntity<String>  get(String url, Map<String,Object> paramMap) {
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        if (paramMap !=null){
            for (String key : paramMap.keySet()){
                builder.queryParam(key,paramMap.get(key));
            }
        }
        //url = builder.build().encode().toUriString();
        url = builder.build().toUriString();
        HttpHeaders headers = new HttpHeaders();
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<MultiValueMap<String, Object>>(null,headers);
        ResponseEntity<String> response = getRestTemplate().exchange(url, HttpMethod.GET, httpEntity, String.class);
        return response;
    }

    public ResponseEntity<String>  postJson(String url, Map<String,Object> bodyMap) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(bodyMap,headers);
        ResponseEntity<String> response = getRestTemplate().exchange(url, HttpMethod.POST, httpEntity, String.class);
        return response;
    }

    public ResponseEntity<String>  postJson(String url,HttpHeaders headers, Map<String,Object> bodyMap) {
        HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(bodyMap,headers);
        ResponseEntity<String> response = getRestTemplate().exchange(url, HttpMethod.POST, httpEntity, String.class);
        return response;
    }

}
